package tp.pr4.gui;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.TitledBorder;

import tp.pr4.Direction;
import tp.pr4.Place;
import tp.pr4.RobotEngine;
import tp.pr4.items.ItemContainer;
import tp.pr4.listeners.AbstractNavigationModuleListener;
import tp.pr4.listeners.AbstractRobotListener;

public class PlaceStatusPanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextArea jText; 
	
	/**
	 * Creates and adds the place information text field to the screen
	 * @param r
	 */
	public PlaceStatusPanel(final RobotEngine r) {
		this.setLayout(new BorderLayout());
		this.add(new JScrollPane(new JTextArea() {
			private static final long serialVersionUID = 1L;
			JTextArea jTextArea = this;
			{
				jText = this;
				r.getNavigationModule().addListener(new AbstractNavigationModuleListener() {
					@Override
					public void onMove(Place newPlace, Place oldPlace, Direction moveDirection) {
						jTextArea.setText(newPlace.toString()); 
					}
				});
				r.addRobotListener(new AbstractRobotListener() {
					@Override
					public void onInventoryChanged(ItemContainer inventory, Place where) {
						jText.setText(where.toString());
					}
				});
			}
		},JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER), BorderLayout.CENTER);
		this.setBorder(new TitledBorder("Log"));
		setPreferredSize(new Dimension(0, 120));
	}
	
	/**
	 * @return the text area this component has
	 */
	public JTextArea getJTextArea() {
		return jText;
	}
}
